package cn.lwq.help;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * @since 2017年7月17日
 * @author lwq 719812998@qq.com
 * @description
*/
public class ConfigRead {
	
	private final String config_file_path = "db.propertie";
	
	public final static String TITLE = "DB JavaFiled (v7.4.1 by.鱼丸[719812998@qq.com])";
	
	public final static String ORACLE = "ORACLE";
	public final static String MYSQL = "MYSQL";
	public final static String NL = "\r\n";
	
	public static String DATABASE_IPADDRESS = "";
	public static String DATABASE_USER = "";
	public static String DATABASE_PASSWORD = "";
	public static String DATABASE_DBNAME = "";
	public static String DATABASE_PORT = "";
	public static String DATABASE_DB = "";
	public static String DATABASE_TABLE = "";
	
	///以下为执行配置
	public static String NOW_DB = "";//当前数据库类型
	public static String NOT_NULL_VALUE = "";//当前非空类型
	public static String TABLE_NAME = "";//表名
	public static boolean ANN_NOTNULL = false;//是否添加非空注解
	public static boolean ANN_LENGTH = false;//是否添加长度注解
	public static boolean ANN_LENGTH_2 = false;//长度是否除2
	public static boolean ANN_DATE = false;//是否添加日期注解
	public static String ANN_DATE_FORMAT = "";//日期格式
	public static boolean HUMP = false;//as 驼峰别名
	
	//消息模版
	public static boolean MESSAGE_NOTNULL = false;
	public static String MESSAGE_NOTNULL_VALUE="参数[%s]不能为空";
	public static boolean MESSAGE_LENGTH = false;
	public static String MESSAGE_LENGTH_VALUE="参数[%s]长度不能超过[%s]";
	
	//以下为格式配置
	public static String FORMAT_JAVA_FIELD = "private %s %s;//%s [%s, %s, %s]";
	public static String FORMAT_MYBATIS_SET = "%s = #{%s,jdbcType=%s},";
	public static String FORMAT_MYBATIS_INSERT = "#{%s,jdbcType=%s},";
	public static String FORMAT_MYBATIS_UPDATE = "    <if test=\"%s\">%s=#{%s,jdbcType=%s},</if>";
	
	//以下为类型配置（符合条件才能添加相应的注解）
	public static String $NOT_NULL = "NOT_NULL";
	public static String $LENGTH = "LENGTH";
	public static String $MAX = "MAX";
	public static String $DATE = "DATE";
	
	/// 读取配置
	public void readConfig() throws Exception {
		File file = new File(config_file_path);
		if(!file.exists()) throw new NullPointerException("没有找到保存记录");
		FileInputStream fis = new FileInputStream(file);
		Properties properties = new Properties();
		properties.load(fis);
		fis.close();
		
		DATABASE_DB = properties.getProperty("database_db");
		DATABASE_IPADDRESS = properties.getProperty("database_ip");
		DATABASE_USER = properties.getProperty("database_user");
		DATABASE_PASSWORD = properties.getProperty("database_password");
		DATABASE_DBNAME = properties.getProperty("database_name");
		DATABASE_PORT = properties.getProperty("database_port");
		DATABASE_TABLE = properties.getProperty("database_table");
	}
	/// 保存配置
	public void saveConfig() throws Exception {
		File file = new File(config_file_path);
		if(!file.exists()) file.createNewFile();
		FileOutputStream fos = new FileOutputStream(file);
		Properties properties = new Properties();
		properties.put("database_db", DATABASE_DB);
		properties.put("database_ip", DATABASE_IPADDRESS);
		properties.put("database_user", DATABASE_USER);
		properties.put("database_password", DATABASE_PASSWORD);
		properties.put("database_name", DATABASE_DBNAME);
		properties.put("database_port", DATABASE_PORT);
		properties.put("database_table", DATABASE_TABLE);
		properties.store(fos, "author: 鱼丸 719812998@qq.com");
		fos.close();
	}
	
	public String getDBUrl(String dbType) throws Exception{
		if(DATABASE_IPADDRESS == null || "".equals(DATABASE_IPADDRESS) ||
			DATABASE_USER == null || "".equals(DATABASE_USER) ||
			DATABASE_PASSWORD == null || "".equals(DATABASE_PASSWORD) ||
			DATABASE_DBNAME == null || "".equals(DATABASE_DBNAME) || 
			DATABASE_PORT == null || "".equals(DATABASE_PORT)) {
			throw new NullPointerException("连接信息有空");
		}
		if(dbType.equals(ORACLE)) {
			return String.format("jdbc:oracle:thin:@%s:%s:%s", DATABASE_IPADDRESS, DATABASE_PORT,DATABASE_DBNAME);
		}
		if(dbType.equals(MYSQL)) {
			return String.format("jdbc:mysql://%s:%s/%s", DATABASE_IPADDRESS, DATABASE_PORT,DATABASE_DBNAME);
		}
		throw new Exception("参数类型不正确");
	}
	
	static String getLengthInt(String num) {
		int i = Integer.parseInt(num);
		String tmp = "";
		if(i > 6) {
			i = i-1;
		}
		for(int j = 0;j<i;j++) {
			tmp += 9;
		}
		return tmp;
	}
	
	public static void main(String[] args) {
		System.out.println(getLengthInt("22"));
	}
	
}
